<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/3/13
 * Time: 11:01
 */

namespace App\Http\Controllers\Api\V1;


use App\Http\Resources\CommentCollection;
use App\Logic\CommonLogic;
use App\Models\Comment;
use App\Models\Doc;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class CommentController extends BaseController
{

    /**
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function get_comment(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'id' => 'required',
            'mode' => ['required', Rule::in(['doc'])]
        ]);
        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }
        $id = $this->getId();
        switch ($request->input("mode")) {
            case 'doc':
                $obj = Doc::query()->findOrFail($id);
                break;
            default:
                $obj = false;
                break;
        }
        if ($obj) {
            $comment = $obj->comments()->orderByDesc('is_hot')->orderByDesc('id');
            $comment->where("commentable_id", $obj->id);
            $comment->where("is_hot", 1);
            $page = $comment->paginate();
            $data = CommentCollection::make($page);
            return $this->success($data);
        } else {
            return $this->failed('error');
        }
    }

    public function add_comment(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'id' => 'required',
            'content' => 'required|max:120',
            'mode' => ['required', Rule::in(['doc'])]
        ]);
        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }
        $content = $request->input('content');
        if (!CommonLogic::msg_sec_check($content)) {
            return $this->failed('内容含有敏感词');
        }
        $id = $this->getId();
        $user = $this->getUser();
        switch ($request->input("mode")) {
            case 'doc':
                $obj = Doc::query()->findOrFail($id);
                break;
            default:
                $obj = false;
                break;
        }
        if ($obj) {
            $comment = $obj->comments()->create([
                'user_id' => $user->id,
                'liveroom_id' => $obj->user->liveroom->id,
                'c_user_id' => $obj->user->id,
                'content' => $content,
                'stick' => 0,
                'is_hot' => 0
            ]);
            $re_data = \App\Http\Resources\Comment::make($comment);
            return $this->success($re_data);
        } else {
            return $this->failed('评论失败');
        }
    }

    public function vote()
    {
        $user = $this->getUser();
        $id = $this->getId();
        $comment = Comment::query()->findOrFail($id);
        if ($comment->isUpvotedBy($user)) {
            $user->cancelVote($comment);
            return $this->message('操作成功', 'off');
        } else {
            $user->upvote($comment);
            return $this->message('操作成功', 'on');
        }

    }

}